<template>
	<view>
		<view class="main">
			<view class="item-row" v-for="(item,index) in goodsList">
				<image class="check-image" v-if="item.check" src="../../static/selected.png" @click="chekAction(index)">
				</image>
				<image class="check-image" v-else src="../../static/noselect.png" @click="chekAction(index)"></image>
				<image class="goods-image" :src="item.image"></image>
				<view class="goods-msg">
					<view class="goods-title">{{item.title}}</view>
					<view>
						<view class="flex-center stock-back">
							<view>销量：{{item.sales}}</view>
							<view class="stock-text">库存：{{item.stock}}</view>
						</view>
						<view class="flex-center-sb">
							<view class="price-text">¥{{item.price}}</view>
							<view class="flex-center">
								<view class="loseAddButton" @click="loseAction(item)">-</view>
								<input class="number-input" v-model="item.number" disabled @input="changeInput(item)" />
								<view class="loseAddButton" @click="addAction(item)">+</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bottomBt">
			<view class="flex-center" @click="allCheckAction">
				<image class="check-image" v-if="allcheck" src="../../static/selected.png">
				</image>
				<image class="check-image" v-else src="../../static/noselect.png"></image>
				<view>全选</view>
				<view class="all-price">¥{{all_price}}</view>
			</view>
			<view class="flex-center">
				<view class="buyBt" @click="buy">立即购买</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "wts-storeCar",
		props: {
			goodsList: []
		},
		data() {
			return {
				number: 1,
				all_price: '0.00',
				checkList: [],
				allcheck: false
			}
		},
		methods: {
			changeInput(item) {
				if (item.number < 1) {
					item.number = 1
				}
			},
			loseAction(item) {
				if (item.number <= 1) {
					return
				}
				item.number--
				let array = []
				for (let i = 0; i < this.goodsList.length; i++) {
					if (this.goodsList[i].check) {
						array.push(this.goodsList[i])
					}
				}
				this.checkList = array
				this.computation()
			},
			addAction(item) {
				item.number++
				let array = []
				for (let i = 0; i < this.goodsList.length; i++) {
					if (this.goodsList[i].check) {
						array.push(this.goodsList[i])
					}
				}
				this.checkList = array
				this.computation()
			},
			// 选择商品
			chekAction(index) {
				this.goodsList[index].check = !this.goodsList[index].check
				let array = []
				for (let i = 0; i < this.goodsList.length; i++) {
					if (this.goodsList[i].check) {
						array.push(this.goodsList[i])
					}
				}
				this.checkList = array
				if (this.checkList.length == this.goodsList.length) {
					this.allcheck = true
				} else {
					this.allcheck = false
				}
				this.computation()
			},
			allCheckAction() {
				this.allcheck = !this.allcheck
				let array = []
				for (let i = 0; i < this.goodsList.length; i++) {
					this.goodsList[i].check = this.allcheck
					if (this.goodsList[i].check) {
						array.push(this.goodsList[i])
					}
				}
				this.checkList = array
				this.computation()
			},
			// 计算总费用
			computation() {
				let price = 0
				this.checkList.forEach(element => {
					price = price + parseFloat(element.price) * element.number
				})
				this.all_price = price.toFixed(2)
			},
			buy() {
				this.$emit('buyAction', JSON.stringify(this.checkList))
			}
		}
	}
</script>

<style lang="scss">
	.main {
		padding-bottom: 220rpx;
	}

	.flex-center {
		display: flex;
		align-items: center;
	}

	.flex-end {
		display: flex;
		align-items: flex-end;
	}

	.flex-center-sb {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.item-row {
		background-color: #fff;
		padding: 30rpx;
		border-bottom: #eee 2rpx solid;
		display: flex;
		align-items: center;

		&:last-of-type {
			border-bottom: none;
		}

		.goods-image {
			width: 200rpx;
			height: 200rpx;
			margin-left: 20rpx;
		}

		.goods-msg {
			margin-left: 20rpx;
			height: 200rpx;
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-title {
				-webkit-line-clamp: 2;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.stock-back {
				color: #666;
				font-size: 26rpx;

				.stock-text {
					margin-left: 30rpx;
				}
			}

			.price-text {
				color: #a20000;
				font-size: 34rpx;
				font-weight: bold;
			}

			.loseAddButton {
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
				line-height: 40rpx;
				text-align: center;
				border: #666 2rpx solid;
				font-size: 30rpx;
			}

			.number-input {
				font-size: 24rpx;
				width: 60rpx;
				height: 40rpx;
				text-align: center;
				background-color: #eee;
				margin: 0 10rpx;
			}
		}
	}

	.check-image {
		width: 45rpx;
		height: 45rpx;
	}

	.bottomBt {
		width: 100%;
		padding: 20rpx;
		box-sizing: border-box;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		border-top: 1px solid #eee;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		z-index: 999;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.buyBt {
			background-color: #a20000;
			color: #fff;
			width: 200rpx;
			height: 76rpx;
			border-radius: 38rpx;
			line-height: 76rpx;
			text-align: center;
		}

		.all-price {
			color: #a20000;
			font-weight: bold;
			margin-left: 20rpx;
			font-size: 34rpx;
		}
	}
</style>